﻿Option Infer On
Imports System
Imports System.IO
Imports System.Net
Imports System.Windows
Imports System.Windows.Data
Imports System.Windows.Media
Imports ChatEngine.Connections
Imports System.Windows.Controls
Imports System.Windows.Navigation
Imports System.Windows.Media.Animation
Imports System.Collections.Specialized
Imports Microsoft.VisualBasic, CoreFX, CoreFX.Logging, CoreFX.Interfaces, ChatEngine.Client.Channel, ChatEngine, CoreFX.API, CoreFX.Controls, ChatEngine.ServerManagement
<Serializable()> _
Partial Public Class ChatViewPage
    Dim NavComplete As Boolean
    Dim HSFStory As Storyboard
    Dim tvSelItem As TreeViewItem
    Dim WithEvents PCM As Essential.UI.PseudoContextMenu = New Essential.UI.PseudoContextMenu
    Dim ChanVar, QueChan As IRCChannel
    Dim TreeviewItem As Windows.Controls.TreeViewItem
    Public MessageArchive As Collections.Generic.Stack(Of String) = New Collections.Generic.Stack(Of String)(75)
    Delegate Sub LoadUsers(ByVal e As Connections.NickListRecvArgs)
    Public Sub New()
        MyBase.New()
        Try
            Me.InitializeComponent()
            Me.ContextMenu = PCM
            AddHandler PCM.ShowPCM, AddressOf FadeInPCM
            AddHandler PCM.HidePCM, AddressOf FadeOutPCM
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
#Region " Properties"
    Public Property Channel() As IRCChannel
        Get
            Return ChanVar
        End Get
        Set(ByVal value As IRCChannel)
            ChanVar = value
        End Set
    End Property
    Public Property BrowserDocumentText() As String
        Get
            Return CType(BrowserHost.Child, RichTextIRC).Text
        End Get
        Set(ByVal value As String)
            CType(BrowserHost.Child, RichTextIRC).AddText(value & vbNewLine)
        End Set
    End Property
    Public WriteOnly Property BrowserVisibility() As Boolean
        Set(ByVal value As Boolean)
            CType(BrowserHost.Child, RichTextIRC).Visible = value
        End Set
    End Property
    Public Property AutoScrollEnabled() As Boolean
        Get
            Return CType(BrowserHost.Child, RichTextIRC).EnableAutoScroll
        End Get
        Set(ByVal value As Boolean)
            CType(BrowserHost.Child, RichTextIRC).EnableAutoScroll = value
        End Set
    End Property
#End Region
#Region " User List Ops"
    Private Sub MoveUser(ByVal Username As String, ByVal Action As ChatEngine.Connections.Action, ByVal ModeGroup As ModeEnum, ByVal ChannelName As String)
        Dim ModeVar As Connections.ModeEnum
        Dim ModeStr As String = ""
        Dim StyleStr As String = ""
        Dim UsrLst As Users.UserList = Nothing
        Dim ChnVar As ChannelItem = SharedVars.Channels.Find(ChannelName)
        UsrLst = ChnVar.ChanObj.UserListProperty
        Select Case ModeGroup
            Case ModeEnum.Admin
                ModeVar = ModeEnum.Admin
                ModeStr = "Admins"
                StyleStr = "ChannelAdmin"
            Case ModeEnum.HalfOP
                ModeVar = ModeEnum.HalfOP
                ModeStr = "Half Operators"
                StyleStr = "ChannelHalfop"
            Case ModeEnum.Op
                ModeVar = ModeEnum.Op
                ModeStr = "Operators"
                StyleStr = "ChannelOp"
            Case ModeEnum.Owner
                ModeVar = ModeEnum.Owner
                ModeStr = "Owners"
                StyleStr = "ChannelOwner"
            Case ModeEnum.User
                ModeVar = ModeEnum.User
                ModeStr = "Users"
                StyleStr = "ChannelUser"
            Case ModeEnum.None
                StyleStr = "ChannelNone"
                ModeVar = ModeEnum.None
                ModeStr = "None"
        End Select
        Dim usItem = UsrLst.Find(Username)
        For I = 0 To UsersList.Items.Count - 1
            For Each Item As TreeViewItem In UsersList.Items.Item(I).Items
                If Item.Header.ToString = Username Then
                    Dim tvTemp As TreeViewItem = Item
                    Dim tvAdmNew = New TreeViewItem
                    tvAdmNew.Header = Username
                    If Not StyleStr = "" Then
                        If Not IsNothing(usItem.nick) Then
                            If Not usItem.mode > ModeVar And Action = Connections.Action.Add Then
                                UsersList.Items.Item(I).Items.Remove(Item)
                                Dim tvAdmin = From tv As TreeViewItem In UsersList.Items Where tv.Header.ToString Like ModeStr
                                ChnVar.ChanObj.UserListProperty.EditItem(usItem, "Mode", Nothing, ModeVar)
                                tvAdmNew.Style = DirectCast(Me.Resources(StyleStr), Style)
                                tvAdmin(0).Items.Add(tvAdmNew)
                                tvAdmin(0).IsExpanded = True
                            ElseIf Action = Connections.Action.Remove Then 'Not usItem.mode > ModeVar And 
                                UsersList.Items.Item(I).Items.Remove(Item)
                                Dim tvAdmin = From tv As TreeViewItem In UsersList.Items Where tv.Header.ToString Like ModeStr.ToString
                                ChnVar.ChanObj.UserListProperty.EditItem(usItem, "Mode", Nothing, ModeVar)
                                tvAdmNew.Style = DirectCast(Me.Resources(StyleStr), Style)
                                tvAdmin(0).Items.Add(tvAdmNew)
                                tvAdmin(0).IsExpanded = True
                            End If
                        End If
                    Else
                        tvAdmNew.Style = Nothing
                    End If
                    Exit For
                End If
            Next
        Next
    End Sub
    Public Sub ChangeUserPic(ByVal Username As String, ByVal Mode As ModeEnum, ByVal Action As ChatEngine.Connections.Action, ByVal ChannelName As String)
        Try
            Select Case Mode
                Case ModeEnum.Admin
                    If Action = ChatEngine.Connections.Action.Add Then
                        MoveUser(Username, Action, ModeEnum.Admin, ChannelName)
                    End If
                    If Action = ChatEngine.Connections.Action.Remove Then
                        MoveUser(Username, Action, ModeEnum.Op, ChannelName)
                    End If
                Case ModeEnum.Op
                    If Action = ChatEngine.Connections.Action.Add Then
                        MoveUser(Username, Action, ModeEnum.Op, ChannelName)
                    End If
                    If Action = ChatEngine.Connections.Action.Remove Then
                        MoveUser(Username, Action, ModeEnum.None, ChannelName)
                    End If
                Case ModeEnum.Owner
                    If Action = ChatEngine.Connections.Action.Add Then
                        MoveUser(Username, Action, ModeEnum.Owner, ChannelName)
                    End If
                    If Action = ChatEngine.Connections.Action.Remove Then
                        MoveUser(Username, Action, ModeEnum.Op, ChannelName)
                    End If
                Case ModeEnum.HalfOP
                    If Action = ChatEngine.Connections.Action.Add Then
                        MoveUser(Username, Action, ModeEnum.HalfOP, ChannelName)
                    End If
                    If Action = ChatEngine.Connections.Action.Remove Then
                        MoveUser(Username, Action, ModeEnum.None, ChannelName)
                    End If
                Case ModeEnum.User
                    If Action = ChatEngine.Connections.Action.Add Then
                        MoveUser(Username, Action, ModeEnum.User, ChannelName)
                    End If
                    If Action = ChatEngine.Connections.Action.Remove Then
                        MoveUser(Username, Action, ModeEnum.None, ChannelName)
                    End If
            End Select
        Catch ex As Exception
            [Global].Services.Get(Of ILogging).AddItem("ChangeUserPic: " & ex.ToString, CoreFX.Logging.MessageType.NetworkLayer)
        End Try
    End Sub
    Public Function CreateItemWithUserPic(ByVal User As String, ByVal Mode As ModeEnum) As TreeViewItem
        Dim tvItem As TreeViewItem = New TreeViewItem
        tvItem.Header = User
        Select Case Mode
            Case ModeEnum.Owner
                tvItem.Style = DirectCast(Me.Resources("ChannelOwner"), Style)
                tvItem.Tag = "Owners"
            Case ModeEnum.Admin
                tvItem.Style = DirectCast(Me.Resources("ChannelAdmin"), Style)
                tvItem.Tag = "Admins"
            Case ModeEnum.Op
                tvItem.Style = DirectCast(Me.Resources("ChannelOp"), Style)
                tvItem.Tag = "Operators"
            Case ModeEnum.HalfOP
                tvItem.Style = DirectCast(Me.Resources("ChannelHalfop"), Style)
                tvItem.Tag = "Half Operators"
            Case ModeEnum.User
                tvItem.Style = DirectCast(Me.Resources("ChannelUser"), Style)
                tvItem.Tag = "Users"
        End Select
        Return tvItem
    End Function
    Public Function ReturnUserCount() As String
        Dim UserCount As Integer = 0
        For I = 0 To UsersList.Items.Count - 1
            For Each Item In UsersList.Items.Item(I).Items
                UserCount = UserCount + 1
            Next
        Next
        Return UserCount.ToString
    End Function
    Public Sub AddSingleUser(ByVal Name As String, ByVal ChannelName As String)
        Try
            Dim tvItem = From tv As TreeViewItem In UsersList.Items Where tv.Header.ToString Like "Users"
            Dim ChnItm = SharedVars.Channels.Find(ChannelName)
            Dim User As ChatEngine.Users.User = New ChatEngine.Users.User
            User.nick = Name
            User.mode = ModeEnum.None
            If Not IsNothing(tvItem(0)) Then
                tvItem(0).Items.Add(CreateItemWithUserPic(Name, ModeEnum.User))
                tvItem(0).IsExpanded = True
                tvItem(0).Tag = "Users"
            End If
            ' If Not IsNothing(ChnItm) Then ChnItm.ChanObj.UserListProperty.Add(User)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Public Sub ChangeNick(ByVal PrevNick As String, ByVal NewNick As String)
        For I = 0 To UsersList.Items.Count - 1
            For Each Item As TreeViewItem In UsersList.Items.Item(I).Items
                If Item.Header.ToString = PrevNick Then
                    Item.Header = NewNick
                    Exit For
                End If
            Next
        Next
    End Sub
    Public Sub LoadUserList(ByVal E As ChatEngine.Connections.NickListRecvArgs)
        'Try
        Dim User As ChatEngine.Users.User
        Dim UserList As ChatEngine.Users.UserList = New ChatEngine.Users.UserList
        ClearUserList()
        LoadCats()
        [Global].Services.Get(Of Interfaces.ILogging).AddItem("LoadUserList: " & E.ListNicks.Length, CoreFX.Logging.MessageType.NetworkLayer)
        For Each Item As String In E.ListNicks
            User = New ChatEngine.Users.User
            [Global].Services.Get(Of Interfaces.ILogging).AddItem("LoaduserList: For channel " & E.ChanneName & Item, CoreFX.Logging.MessageType.NetworkLayer)
            If Item <> "" Then
                If Item.Contains(":") Then Item = Item.Replace(":", "")
                If Not Item.Contains("~") And Not Item.Contains("&") And Not Item.Contains("@") And Not Item.Contains("%") And Not Item.Contains("+") And Not UsersList.Items.Contains(Trim(Item)) Then
                    If FindUser(Item) = False Then
                        Dim tvItem = From tv As TreeViewItem In UsersList.Items Where tv.Header.ToString Like "None"
                        If Not IsNothing(tvItem(0)) Then
                            tvItem(0).Items.Add(CreateItemWithUserPic(Item, ModeEnum.None))
                            tvItem(0).IsExpanded = True
                            User.mode = ModeEnum.None
                            User.nick = Item
                        End If
                    End If
                End If
                If Item.Contains("~") Then
                    Item = Item.Replace("~", "")
                    If FindUser(Item) = False Then
                        Dim tvItem = From tv As TreeViewItem In UsersList.Items Where tv.Header.ToString Like "Owners"
                        If Not IsNothing(tvItem(0)) Then
                            tvItem(0).Items.Add(CreateItemWithUserPic(Item, ModeEnum.Owner))
                            tvItem(0).IsExpanded = True
                            User.mode = ModeEnum.Owner
                            User.nick = Item
                        End If
                    End If
                End If
                If Item.Contains("&") Then
                    Item = Item.Replace("&", "")
                    If FindUser(Item) = False Then
                        Dim tvItem = From tv As TreeViewItem In UsersList.Items Where tv.Header.ToString Like "Admins"
                        If Not IsNothing(tvItem(0)) Then
                            tvItem(0).Items.Add(CreateItemWithUserPic(Item, ModeEnum.Admin))
                            tvItem(0).IsExpanded = True
                            User.mode = ModeEnum.Admin
                            User.nick = Item
                        End If
                    End If
                End If
                If Item.Contains("@") Then
                    Item = Item.Replace("@", "")
                    If FindUser(Item) = False Then
                        Dim tvItem = From tv As TreeViewItem In UsersList.Items Where tv.Header.ToString Like "Operators"
                        If Not IsNothing(tvItem(0)) Then
                            tvItem(0).Items.Add(CreateItemWithUserPic(Item, ModeEnum.Op))
                            tvItem(0).IsExpanded = True
                            User.mode = ModeEnum.Op
                            User.nick = Item
                        End If
                    End If
                End If
                If Item.Contains("%") Then
                    Item = Item.Replace("%", "")
                    If FindUser(Item) = False Then
                        Dim tvItem = From tv As TreeViewItem In UsersList.Items Where tv.Header.ToString Like "Half Operators"
                        If Not IsNothing(tvItem(0)) Then
                            tvItem(0).Items.Add(CreateItemWithUserPic(Item, ModeEnum.HalfOP))
                            tvItem(0).IsExpanded = True
                            User.mode = ModeEnum.HalfOP
                            User.nick = Item
                        End If
                    End If
                End If
                If Item.Contains("+") Then
                    Item = Item.Replace("+", "")
                    If FindUser(Item) = False Then
                        Dim tvItem = From tv As TreeViewItem In UsersList.Items Where tv.Header.ToString Like "Users"
                        If Not IsNothing(tvItem(0)) Then
                            tvItem(0).Items.Add(CreateItemWithUserPic(Item, ModeEnum.User))
                            tvItem(0).IsExpanded = True
                            User.mode = ModeEnum.User
                            User.nick = Item
                        End If
                    End If
                End If
                UserList.Add(User)
            End If
        Next
        Dim chan As ChannelItem = SharedVars.Channels.Find(E.ChanneName)
        chan.ChanObj.UserListProperty = UserList
    End Sub
    Public Function FindUser(ByVal Username As String) As Boolean
        For I = 0 To UsersList.Items.Count - 1
            For Each Item As TreeViewItem In UsersList.Items.Item(I).Items
                If Item.Header.ToString = Username Then
                    Return True
                    Exit For
                End If
            Next
        Next
        Return False
    End Function
    Public Sub RemoveUserFromList(ByVal User As String)
        Try
            For I = 0 To UsersList.Items.Count - 1
                For Each Item As TreeViewItem In UsersList.Items.Item(I).Items
                    If Item.Header.ToString = User Then
                        UsersList.Items.Item(I).Items.Remove(Item)
                        Exit For
                    End If
                Next
            Next
        Catch ex As Exception
            [Global].Services.Get(Of ILogging).AddItem("RemoveUserFromList: " & ex.ToString, CoreFX.Logging.MessageType.General)
        End Try
    End Sub
    Public Sub ClearUserList()
        UsersList.Items.Clear()
        LoadCats()
    End Sub
#End Region
    Public Sub FadeInPCM()
        HSFStory = DirectCast(Me.Resources("FadeInPCM"), Storyboard)
        HSFStory.Begin(Me)
    End Sub
    Public Sub FadeOutPCM()
        HSFStory = DirectCast(Me.Resources("BlurOut"), Storyboard)
        HSFStory.Begin(Me)
    End Sub
    Public Sub RenderNotice(ByVal Message As String)
        CType(BrowserHost.Child, RichTextIRC).Notice(Message)
    End Sub
    Public Sub RenderChannelMsg(ByVal Message As String)
        Try
            CType(BrowserHost.Child, RichTextIRC).ChannelMessage(Message)
        Catch ex As Exception
            [Global].Services.Get(Of ILogging).AddItem("RenderChannelMsg: " & ex.ToString, MessageType.General)
        End Try
    End Sub
    Public Sub RenderKicked(ByVal Message As String)
        'BrowserDocumentText = Message
        CType(BrowserHost.Child, RichTextIRC).Kicked(Message)
    End Sub
    Public Sub LoadCats()
        Dim tvFind = From tv As TreeViewItem In UsersList.Items Where tv.Header.ToString Like "Owners"
        Dim tvItem, tvItem1, tvItem2, tvItem3, tvItem4, tvItem5 As TreeViewItem
        If IsNothing(tvFind(0)) Then
            tvItem = New TreeViewItem
            tvItem.Header = "Owners"
            tvItem1 = New TreeViewItem
            tvItem1.Header = "Admins"
            tvItem2 = New TreeViewItem
            tvItem2.Header = "Operators"
            tvItem3 = New TreeViewItem
            tvItem3.Header = "Half Operators"
            tvItem4 = New TreeViewItem
            tvItem4.Header = "Users"
            tvItem5 = New TreeViewItem
            tvItem5.Header = "None"
            Me.UsersList.Items.Add(tvItem)
            Me.UsersList.Items.Add(tvItem1)
            Me.UsersList.Items.Add(tvItem2)
            Me.UsersList.Items.Add(tvItem3)
            Me.UsersList.Items.Add(tvItem4)
            Me.UsersList.Items.Add(tvItem5)
        End If
    End Sub
    Public Sub RenderQuit(ByVal Message As String)
        CType(BrowserHost.Child, RichTextIRC).IRCQuit(Message)
    End Sub
    Private Sub ChatViewPage_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Initialized
    End Sub
    Private Sub ChatViewPage_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        LoadCats()
    End Sub
    Private Sub BrowserHost_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles BrowserHost.Loaded
    End Sub
    Private Sub UsersList_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles UsersList.MouseDoubleClick
        If e.ChangedButton = Input.MouseButton.Left Then
            Dim ircChan As ChannelItem
            tvSelItem = UsersList.SelectedItem
            ircChan = New ChannelItem
            QueChan = New IRCChannel(tvSelItem.Header)
            ircChan.ChanObj = Channel
            ircChan.Name = tvSelItem.Header
            SharedVars.Channels.Add(ircChan)
            If Not IsNothing(tvSelItem) Then
                Global.mchatstart.Window1.InitQuery(tvSelItem.Header)
                ' CType(QueChan.ChannelWindow, mchatstart.Window1).InitQuery(tvSelItem.Header)
            End If
        End If
    End Sub
    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub
    Private Sub UsersList_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles UsersList.MouseDown
        If e.ChangedButton = Input.MouseButton.Right Then
            MsgBox("UserList_MouseDown: " & UsersList.SelectedItem.Header.ToString)
        End If
    End Sub
End Class